package com.bbh.plugin.single.privilege.demo.controller;

import com.bbh.plugin.single.privilege.core.entity.App;
import com.bbh.plugin.single.privilege.core.entity.OperateLog;
import com.bbh.plugin.single.privilege.core.enums.BaseEnum;
import com.bbh.plugin.single.privilege.core.service.AppService;
import com.bbh.plugin.single.privilege.core.service.OperateLogService;
import com.bbh.common.base.vo.GridVo;
import com.bbh.common.domain.Condition;
import com.bbh.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import com.bbh.common.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/operateLog")
public class OperateLogController extends BasicController {
    @Autowired
    private AppService appService;
    @Autowired
    private OperateLogService operateLogService;


    @RequestMapping(value = VIEW_FILED + "/index", method = RequestMethod.GET)
    public String index(ModelMap model) {
        Condition condition = new Condition();
        condition.putEquals("dataStatus", BaseEnum.DATA_STATUS.NORMAL.getKey());
        condition.putEquals("isEnabled", BaseEnum.IS_ENABLED.ENABLED.getKey());
        List<App> appList = appService.selectList(condition, null);
        model.addAttribute("appList", appList);
        return "single/operateLog/operateLog";
    }

    /**
     *
     */
    @RequestMapping(value = VIEW_FILED + "/list")
    @ResponseBody
    public GridVo list(HttpServletRequest request, Date startTime, Date endTime, String appKey, String ip, String tag, String user, String method) throws Exception {
        Condition params = new Condition();
        GridVo vo = new GridVo();
        params.putNotBlank("startTime", DateUtils.getStartDate(startTime));
        params.putNotBlank("endTime", DateUtils.getEndDate(endTime));
        params.putNotBlank("appKey", appKey);
        params.putNotBlank("ip", ip);
        params.putNotBlank("tag", tag);
        params.putNotBlank("user", user);
        params.putNotBlank("method", method);
        Page<OperateLog> pageResult = operateLogService.selectPage(params, initPageRequest(request, PAGE_NUMBER, PAGE_ROWS));
        return new GridVo(pageResult);
    }
}